<?php


namespace App\Http\Controllers\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\System\Dictionary\Customs\StoreRequest;
use App\Http\Requests\System\Dictionary\Customs\UpdateRequest;
use App\Http\Searchs\System\Dictionary\CustomsSearch;
use App\Repositories\Customs;

class CustomsController extends Controller
{
    /**
     * @var Customs
     * @author luffyzhao@vip.126.com
     */
    private $customs;

    /**
     * CustomsController constructor.
     * @param Customs $customs
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Customs $customs)
    {
        $this->customs = $customs;
    }

    /**
     * @param CustomsSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(CustomsSearch $search)
    {
        return $this->response(
            $this->customs->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->customs->create($request->only(['code', 'name']))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->customs->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->customs->update($id, $request->only(['name']))
        );
    }
}